﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CH.TestData.Common;
using System.IO;
using System.Xml.Linq;
using System.Data;
using System.Xml;
using System.Threading;
using CH.Easy.Utility;
namespace CH.TestData.Provider
{


    [DataTypeName("字典数据")]
    public class ListProvider : IDataProvider
    {

        private int nextIndex;
        private string path;
        private GenerateOrder generateOrder;
        private string columnName;
        private List<string> dataSource;
        private void ReSetNextIndex()
        {
            switch (this.generateOrder)
            {
                case GenerateOrder.ASC:
                    this.nextIndex = 0;
                    break;
                case GenerateOrder.DESC:
                    this.nextIndex = this.dataSource.Count - 1;
                    break;
                case GenerateOrder.Random:
                    this.nextIndex = new Random(Guid.NewGuid().GetHashCode()).Next(0, this.dataSource.Count - 1);
                    break;
                default:
                    break;
            }
        }

    

        public void CreateOption(FlowLayoutPanel flp)
        {
            Label lbltype = new Label();
            lbltype.Text = "类型";
            lbltype.Width = 30;
            lbltype.Margin = new Padding(0, 7, 0, 0);
            flp.Controls.Add(lbltype);

            ComboBox cbType = new ComboBox();
            cbType.DataSource = LoadData();
            cbType.DisplayMember = "Name";
            cbType.Name = "cbType";
            cbType.ValueMember = "Name";
            cbType.DropDownStyle = ComboBoxStyle.DropDownList;
            cbType.Width = 200;
            flp.Controls.Add(cbType);

            ComboBox cbGenerateOrder = new ComboBox();
            cbGenerateOrder.DataSource = new List<KeyValuePair<string, string>> 
            {
                new KeyValuePair<string,string>(((int)GenerateOrder.ASC).ToString(), "升序"),
                new KeyValuePair<string,string>(((int)GenerateOrder.DESC).ToString(), "降序"),
                new KeyValuePair<string,string>(((int)GenerateOrder.Random).ToString(), "随机")
            };


            cbGenerateOrder.Width = 60;
            cbGenerateOrder.DisplayMember = "Value";
            cbGenerateOrder.ValueMember = "Key";
            cbGenerateOrder.Name = "cbGenerateOrder";
            cbGenerateOrder.DropDownStyle = ComboBoxStyle.DropDownList;
            flp.Controls.Add(cbGenerateOrder);

        }

        public void SetDataSource(FlowLayoutPanel flp)
        {
            TextBox txtColumnName = flp.Controls.Find("txtColumnName", false).First() as TextBox;
            this.columnName = txtColumnName.Text;

            ComboBox cbType = flp.Controls.Find("cbType", true).First() as ComboBox;
            this.path = AppDomain.CurrentDomain.BaseDirectory + @"Data\" + cbType.SelectedValue;
            ComboBox cbGenerateOrder = flp.Controls.Find("cbGenerateOrder", true).First() as ComboBox;
            this.generateOrder = (GenerateOrder)int.Parse(cbGenerateOrder.SelectedValue.ToString());
          


            this.dataSource = new List<string>();
            XDocument xdoc = XDocument.Load(this.path);
            XElement root = xdoc.Root;
            if (root.Element("Type").Value.ToLower() == "sql")
            {
                string sql = root.Element("Sql").Value;
                string connectionString = root.Element("ConnectionString").Value;
                DataTable dt = DbSqlHelper.ExecuteDataTable(connectionString, CommandType.Text, sql);
                if (dt != null)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        this.dataSource.Add(row[0].ToString());
                    }
                }

            }
            else
            {
                this.dataSource = root.Element("List").Elements("Item").Select(p => p.Value).ToList();
            }

            ReSetNextIndex();
        }
        public string GetNextValue()
        {
            if (this.dataSource.Count == 0 || this.dataSource.Count - 1 < this.nextIndex)
            {
                return "";
            }

            int index = this.nextIndex;
            //设置下一条记录索引
            switch (this.generateOrder)
            {
                case GenerateOrder.ASC:
                    this.nextIndex++;
                    if (nextIndex > this.dataSource.Count - 1)
                    {
                        ReSetNextIndex();
                    }
                    break;
                case GenerateOrder.DESC:
                    this.nextIndex--;
                    if (nextIndex < 0)
                    {
                        ReSetNextIndex();
                    }
                    break;
                case GenerateOrder.Random:
                    this.nextIndex = new Random(Guid.NewGuid().GetHashCode()).Next(0, this.dataSource.Count - 1);
                    break;
                default:
                    break;
            }

            string value = this.dataSource[index];
            if (value.IsNumber())
            {
                return value;
            }
            else
            {
                return "N'" + value + "'";
            }

        }

        private FileInfo[] LoadData()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"Data\";
            DirectoryInfo dir = new DirectoryInfo(path);
            return dir.GetFiles("*.xml");
        }


    }
}
